package com.example.zhihuhelpapp.base;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.LayoutRes;
import androidx.appcompat.widget.Toolbar;

import com.example.zhihuhelpapp.R;
import com.example.zhihuhelpapp.event.UIEvent;
import com.pgyersdk.crash.PgyCrashManager;
import com.trello.rxlifecycle.components.support.RxAppCompatActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public abstract class BaseActivity extends RxAppCompatActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
        EventBus.getDefault().register(this);
        PgyCrashManager.register();
        initView();
        initData();
    }

    protected void showToolBar(boolean isShowBack,String title){
        Toolbar toolbar = findViewById(R.id.toolBar);
        if (toolbar == null) {
            return;
        }
        if (isShowBack) {
            toolbar.setNavigationIcon(R.drawable.icon_back);
            toolbar.setNavigationOnClickListener(v -> finish());
        }
        toolbar.setTitle(title);
    }

    protected abstract @LayoutRes
    int getLayoutId();

    protected abstract void initView();

    protected void initData() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        PgyCrashManager.unregister();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onUiEvent(UIEvent uiEvent) {
    }
}
